//
//  RateQueryVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2018/2/26.
//  Copyright © 2018年 imguider. All rights reserved.
//

#import "RateQueryVC.h"
#import "RateQueryCell.h"
#import "RateListVC.h"

@interface RateQueryVC ()
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (nonatomic, strong) NSArray *rateArray;

@property (nonatomic, strong) NSString *value;

@end

@implementation RateQueryVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = MYLocalizedString(@"EXCHANGE_RATE_RESEARCH", nil);
    
    UIView *lineView = [UIView new];
    lineView.backgroundColor = [UIColor clearColor];
    lineView.frame = CGRectMake(0, 0, kScreenWidth, 8);
    
    self.tableView.tableHeaderView = lineView;
    
    [ActivityIndicatorView showHUDAddedTo:self.view backgroundColor:[UIColor whiteColor]];
    [self loadData];
}

- (void)loadData {
    
    
    if ([CountryManager sharedManager].rateArray.count > 0) {
        [self reloadDataSource];
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        return;
    }
    
    @weakify(self);
    [NetAPIManager getRateListWithBlock:^(NSArray *rateArray, NSError *error) {
        @strongify(self);
        
        [ActivityIndicatorView hideHUDAddedTo:self.view];
        [CountryManager sharedManager].rateArray = rateArray;
        
        [self reloadDataSource];
        
    }];
}

- (void)reloadDataSource {
    
    NSMutableArray *resultRateArray = [NSMutableArray array];
    
    for (NSString *code in [CountryManager sharedManager].saveRateArray) {
        
        for (RateMode *orignMode in [CountryManager sharedManager].rateArray) {
            
            if ([orignMode.code isEqualToString:code]) {
                [resultRateArray addObject:orignMode];
                break;
            }
        }
    }
    
    self.rateArray = resultRateArray;
    
    [self.tableView reloadData];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.rateArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    RateQueryCell *cell = [tableView dequeueReusableCellWithIdentifier:@"RateQueryCell" forIndexPath:indexPath];
    
    RateMode *mode = self.rateArray[indexPath.section];
    
    mode.baseMoney = self.value;
    
    cell.delegate = self;
    cell.mode = mode;
    
    return cell;
}


- (void)rateQueryCell:(RateQueryCell *)cell changedValue:(NSString *)value {
    
    NSIndexPath *indexpath = [self.tableView indexPathForCell:cell];
    
    RateMode *mode = self.rateArray[indexpath.section];
    
    NSString *realMoney = value.length == 0 ? @"" : @(value.floatValue / mode.rate.floatValue).stringValue;
    
    mode.baseMoney = realMoney;
    
    self.value = realMoney;
    
    [[NSNotificationCenter defaultCenter] postNotificationName:kRateModelValueChangeNotif object:nil userInfo:@{kRateModelValueChangeKey:mode}];
}

- (void)rateQueryCell:(RateQueryCell *)cell changedMode:(RateMode *)mode {
    
    RateListVC *listVC = [RateListVC new];
    listVC.selectMode = mode;
    listVC.delegate = self;
    
    [self.navigationController pushViewController:listVC animated:YES];
    
}

- (void)selectedComplete {
    
    [self loadData];
}

@end
